package com.jxc.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Damagelist;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VDamagelist;
import com.jxc.service.DamagelistServiceI;

@Service("damagelistService")
public class DamagelistServiceImpl implements DamagelistServiceI {

	@Autowired
	private BaseDaoI<Damagelist> damagelistDao;

	@Override
	public List<VDamagelist> combobox(VDamagelist vdl) {
		return this.changeModel(this.find(vdl));
	}

	@Override
	public DataGrid datagrid(VDamagelist vdl) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vdl)));
		dg.setTotal(this.total(vdl));
		return dg;
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					Damagelist dl = damagelistDao.get(Damagelist.class, Long.parseLong(id.trim()));
					if (dl != null) {
						damagelistDao.delete(dl);
					}
				}
			}
		}
	}

	@Override
	public Long save(VDamagelist vdl) {
		Damagelist dl = new Damagelist();
		BeanUtils.copyProperties(vdl, dl);
		if (vdl.getUid() != null) {
			dl.setUser(new User(vdl.getUid()));
		}
		if (vdl.getRid() != null) {
			dl.setRepertory(new Repertory(vdl.getRid()));
		}
		if (vdl.getCuid() != null) {
			dl.setConfirmUser(new User(vdl.getCuid()));
		}
		if(vdl.getDate()!=null){
			java.sql.Timestamp time = new java.sql.Timestamp(new java.util.Date().getTime());
			int h = time.getHours();//时
			int f = time.getMinutes();//分
			int m = time.getSeconds();//秒
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			//年月日
			int y = vdl.getDate().getYear()+1900;
			int yf = vdl.getDate().getMonth()+1;
			int day = vdl.getDate().getDate();
			String str = y+"-"+yf+"-"+day+" "+h+":"+f+":"+m;	
			try {
				Date d = sdf.parse(str);
				dl.setDate(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		this.damagelistDao.save(dl);
		return dl.getId();
	}

	@Override
	public Long update(VDamagelist vdl) {
		Damagelist dl = damagelistDao.get(Damagelist.class, vdl.getId());
		BeanUtils.copyProperties(vdl, dl, new String[] { "id" });
		if (vdl.getUid() != null) {
			dl.setUser(new User(vdl.getUid()));
		}
		if (vdl.getRid() != null) {
			dl.setRepertory(new Repertory(vdl.getRid()));
		}
		if (vdl.getCuid() != null) {
			dl.setConfirmUser(new User(vdl.getCuid()));
		}
		this.damagelistDao.update(dl);
		return dl.getId();
	}

	private List<VDamagelist> changeModel(List<Damagelist> dls) {
		List<VDamagelist> vdls = new ArrayList<VDamagelist>();
		if (dls != null && dls.size() > 0) {
			for (Damagelist dl : dls) {
				VDamagelist vdl = new VDamagelist();
				BeanUtils.copyProperties(dl, vdl);
				if (dl.getUser() != null) {
					User user = dl.getUser();
					vdl.setUid(user.getId());
					vdl.setUname(user.getUsername());
				}
				if (dl.getConfirmUser() != null) {
					User cu = dl.getConfirmUser();
					vdl.setCuid(cu.getId());
					vdl.setCuname(cu.getUsername());
				}
				if (dl.getRepertory() != null) {
					Repertory repertory = dl.getRepertory();
					vdl.setRid(repertory.getId());
					vdl.setRname(repertory.getName());
				}
				vdls.add(vdl);
			}
		}
		return vdls;
	}

	private List<Damagelist> find(VDamagelist vdl) {
		String hql = "from Damagelist t where 1=1 ";

		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdl, hql, values);

		if (vdl.getSort() != null && vdl.getOrder() != null) {
			hql += " order by " + vdl.getSort() + " " + vdl.getOrder();
		}
		return damagelistDao.find(hql, values, vdl.getPage(), vdl.getRows());
	}

	private Long total(VDamagelist vdl) {
		String hql = "select count(*) from Damagelist t where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		hql = addWhere(vdl, hql, values);
		return damagelistDao.count(hql, values);
	}

	private String addWhere(VDamagelist vdl, String hql, List<Object> values) {
		if (vdl.getId() != null) {
			hql += " and t.id = ? ";
			values.add(vdl.getId());
		}
		if (vdl.getSerial() != null) {
			hql += " and t.serial like ? ";
			values.add("%%"+vdl.getSerial()+"%%");
		}

		if (vdl.getBeginDate() != null) {
			hql += " and t.date >= ? ";
			values.add(vdl.getBeginDate());
		}

		if (vdl.getEndDate() != null) {
			hql += " and t.date <= ? ";
			values.add(vdl.getEndDate());
		}

		if (vdl.getUid() != null) {
			hql += " and t.user.id = ? ";
			values.add(vdl.getUid());
		}

		if (vdl.getRid() != null) {
			hql += " and t.repertory.id = ? ";
			values.add(vdl.getRid());
		}

		if (vdl.getCuid() != null) {
			hql += " and t.confirmUser.id = ? ";
			values.add(vdl.getCuid());
		}
		return hql;
	}

}
